<?php
namespace BoxSns\Hooks;
class TopicHooks extends \HooksController {

	//帖子详情
	function topic_load_eduGroup(&$param) {
		if ($param['pid']) {
			$param['pidData'] = BoxModel("sns_group")->where(array('id' => $param['pid']))->find();
		}
	}

	//评论
	function comment_after_save_eduGroup(&$params) {
		$topicData = BoxModel("sns_topic")->where(array('id' => $params['pid']))->find();
		$title = $topicData['title'] ? $topicData['title'] : $topicData['content'];
		$user_data = BoxModel("system_member")->where(array('user_id' => $params['user_id']))->find();
		$nickname = $user_data['nickname'] ? $user_data['nickname'] : "有人";
		/****最后回复用户**/
		BoxModel("sns_topic")->where(array('id' => $params['pid']))->save(array('lastPostName' => $user_data['nickname'], 'lastupdate' => time()));
		$url = HomeUrl . U('Group/topic', array('id' => $params['pid'], 'comment_id' => $params['id']));
		/******提醒楼主***/
		if ($topicData['user_id'] != $this->user_id) {
			$NotiParams = array(
				'user_id' => $topicData['user_id'],
				'url' => $url,
				'name' => $nickname,
				'title' => cutstr(htmlspecialchars($title), 50),
			);
			Logic("BoxSns", "Noti")->send("00004", $NotiParams);
			//微信提醒
			$wx_param = array(
				'user_id' => $topicData['user_id'],
				'title' => $nickname . "在社区回复了您帖子",
				'topic' => cutstr(strip_tags($title), 50),
				'info' => "评论内容：" . cutstr(strip_tags($params['content']), 80),
				'url' => $url,
			);
			//Logs("wx_msg", $param);exit();
			thread("BoxApi/Index/wx_msg", $wx_param);

		}
		/*******提醒AT***/
		$user_ids = Logic("BoxSns", "Noti")->At($params['content']); //需要@提醒的用户ID
		unset($user_ids[$this->user_id]);
		if (!empty($user_ids)) {
			$NotiParams = array(
				'user_id' => $user_ids,
				'url' => $url,
				'name' => $nickname,
				'title' => cutstr(htmlspecialchars($title), 50),
			);
			Logic("BoxSns", "Noti")->send("00005", $NotiParams);
			//微信提醒
			$wx_param = array(
				'user_id' => $user_ids,
				'title' => $nickname . "在社区@了你",
				'topic' => cutstr(strip_tags($title), 50),
				'info' => "回复内容：" . cutstr(strip_tags($params['content']), 80),
				'url' => $url,
			);
			//Logs("wx_msg", $param);exit();
			thread("BoxApi/Index/wx_msg", $wx_param);
		}
		/**********回复提醒****/
		if (!empty($params['to_comment_id']) && $params['to_user_id'] != $this->user_id && $topicData['user_id'] != $params['to_user_id']) {
			$NotiParams = array(
				'user_id' => $params['to_user_id'],
				'url' => $url,
				'name' => $nickname,
				'title' => cutstr(htmlspecialchars($title), 50),
			);
			//	dump($NotiParams);
			Logic("BoxSns", "Noti")->send("00006", $NotiParams);
			//微信提醒
			$wx_param = array(
				'user_id' => $params['to_user_id'],
				'title' => $nickname . "在社区回复了你的评论",
				'topic' => cutstr(strip_tags($title), 50),
				'info' => "回复内容：" . cutstr(strip_tags($params['content']), 80),
				'url' => $url,
			);
			//Logs("wx_msg", $param);exit();
			thread("BoxApi/Index/wx_msg", $wx_param);
		}
		/***回复获得学分*/
		// $row = array(
		// 	'user_id' => $params['user_id'],
		// 	'url' => $url,
		// 	'title' => cutstr(htmlspecialchars($title), 50),
		// 	'info' => cutstr($params['content'], 400),
		// 	'event' => "00005",
		// 	'subjectid' => $topicData['pid'],
		// );
		// //dump($row);
		// $r = Logic("BoxEdu", "User")->add_xuebazi($row);
	}

}
